﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using makitRS.Core.Models.Reports;
using makitRS.Core.Reports;
using System.Data.Entity.Validation;
using System.Diagnostics;

namespace makitRS.Core.Data
{
    public class SampleDataInitializer : DropCreateDatabaseIfModelChanges<ReportList>
    {
        protected override void Seed(ReportList context)
        {
            try
            {

                SubCategoriesCategory rootCat = new SubCategoriesCategory();
                rootCat.CategoryID = 1;
                rootCat.CategoryName = "Sales Data";
                rootCat.SubCategories = new List<Category>();

                ReportsCategory brandACat = new ReportsCategory();
                brandACat.CategoryID = 2;
                brandACat.CategoryName = "Brand A";

                rootCat.SubCategories.Add(brandACat);

                ReportsCategory brandBCat = new ReportsCategory();
                brandBCat.CategoryID = 3;
                brandBCat.CategoryName = "Brand B";
                brandBCat.Reports = new List<Report>();

                DownloadableReport demoRep1 = new DownloadableReport();
                demoRep1.ReportID = 1;
                demoRep1.ReportName = "Sales Per Day Over 30 Days";
                demoRep1.ReportCategory = brandBCat;
                demoRep1.ReportDateTime = DateTime.Now;
                demoRep1.FileName = "20110723_SalesPerDay.csv";
                demoRep1.ContentType = "text/plain";

                brandBCat.Reports.Add(demoRep1);
                rootCat.SubCategories.Add(brandBCat);

                context.Reports.Add(demoRep1);
                context.Categories.Add(brandACat);
                context.Categories.Add(brandBCat);
                context.Categories.Add(rootCat);

                context.SaveChanges();

            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }
        }
    }
}
